<?php
/**
 * Created by IDEA.
 * User: dinesh
 * Date: Mar 26, 2012
 * Time: 12:09:28 AM
 */
    class AdodbDriver{
        private $dbConfig; 
        public $db;
        public function __construct(){
            include("libraries/adodb5/adodb-exceptions.inc.php");
            include('libraries/adodb5/adodb.inc.php');
            $this->dbConfig = CmbBaseConfig::getConfig("db");
            $ADODB_ACTIVE_CACHESECS = 60;
        }
        public function pConnect($debug=false){        
            $this->db = ADONewConnection($this->dbConfig['driver']); # eg 'mysql' or 'postgres'
            $this->db->debug = $debug;
            $this->db->PConnect($this->dbConfig['host'], $this->dbConfig['username'], $this->dbConfig['password'], $this->dbConfig['database']);
            return $this->db;
        }
         public function connect($debug=false){
            $this->db = ADONewConnection($this->dbConfig['driver']); # eg 'mysql' or 'postgres'
            $this->db->debug = $debug;
            $this->db->Connect($this->dbConfig['host'], $this->dbConfig['username'], $this->dbConfig['password'], $this->dbConfig['database']);
            return $this->db;
        }
        public function dnsConnect(){
            $dsn = $this->dbConfig['driver'].'://'.$this->dbConfig['username'].':'.$this->dbConfig['password'].'@'.$this->dbConfig['host'].'/'.$this->dbConfig['database'];//root:123@localhost/aea_mis_dev
            $this->db = NewADOConnection($dsn);
            return $this->db;
        }





        public function runSQL($sql){         
         $rs = $this->db->Execute($sql);
         print $rs->GetMenu2('theme','','0:--selected--','','','id="theme"');
        // print "<pre>";
        // print_r($rs->GetRows());
        // print "</pre>";

        }
        public function autoExe(){
            $table = 'aea_user';
            $record["user_name"] = "Bob";
            $record["password"] = "Smith";
            $record["profile_id"] = "1";
            $record["create_user_id"] = 1;
            $record["update_user_id"] = 1;
            $record["create_time"] = time();
            $insertSQL = $this->db->AutoExecute( $table, $record, 'INSERT');
        }
        public function activeRecode(){
            $db = dnsConnect();//NewADOConnection('mysql://root:123@localhost/aea_mis_dev');
            ADOdb_Active_Record::SetDatabaseAdapter($db);
            /*$db->Execute("CREATE TEMPORARY TABLE `persons` (
                            `id` int(10) unsigned NOT NULL auto_increment,
                            `name_first` varchar(100) NOT NULL default '',
                            `name_last` varchar(100) NOT NULL default '',
                            `favorite_color` varchar(100) NOT NULL default '',
                            PRIMARY KEY  (`id`)
                        ) ENGINE=MyISAM;
                       "); */
         /*   $user = new User();
            $user->user_name = "dineshpradeep";
            $user->password = '123';
            $user->create_user_id = 2;
            $user->update_user_id = 1;
            $user->profile_id = 1;
          */
          //  $user2 = new User();
          //  $user3 = new User();
            try {
              // $userArray = $user3->Find("user_name like ? order by id", array('di%'));
              // foreach($userArray as $us){
                //  echo $us->user_name.'<br/>';
             //  }
                ///////////////////replace////////////////
               // $user->id=1;
               // $user->replace();


               ////////////////////////////////////

               //////////////relationship////////////////////
               ADODB_Active_Record::ClassHasMany('Profile','userid','profile_id','User');

             //  $profile = new Profile();
             //  $profile->Load("id=1");
              //     foreach($profile->userid as $u) {
              //         echo " $u->user_name";
              //         $u->password= ' Kpsl.';
               //        $u->Save();  ## each child record must be saved individually
               //    }

               ///create new perant and save chile////////
               /*
               $profile1 = new Profile();
               $profile1->userid = new User();
               $profile1->full_name='kpsl2';
               $profile1->create_user_id = 1;
                $profile1->update_user_id = 1;
               $profile1->save();

               $profile1->userid->user_name = "user2222";
                $profile1->userid->password = '123';
                $profile1->userid->create_user_id = 1;
                $profile1->userid->update_user_id = 1;
                 $profile1->userid->profile_id=$profile1->id;
                  $profile1->userid->save();
                */
               //////////////////////////////////////////////////

               //  $user2->load("id=?", array(1));
               //  echo $user2->user_name;
                //$user->save();
               // echo "new user id is".$user->id;

                ////////////////////////class belong to ///////////
              /*
               ADODB_Active_Record::ClassBelongsTo('User','profile','profile_id','id','Profile');
               $us = new User(); // default tablename will be 'kids', with primary key 'id'
                   $us->Load('id=20');
                   $p = $us->profile;
                   if (!$p ) echo "Error loading belongsTo<br>";
                   else echo  $p->full_name."  -----OK loading BelongTo<br>";
                */


                //////////////////////////////////////////

                /////////////get active recode ////////////////
              /* $table = 'aea_profile';
               $whereOrderBy = "full_name LIKE 'k%' ORDER BY id";
               $activeRecArr = $db->GetActiveRecords($table, $whereOrderBy);
               foreach($activeRecArr as $rec) {
                   $rec->init_name = rand();
                   $rec->save();
               }
               */


                //////////////////////////////
            } catch (exception $e) {
               //var_dump($e);
                print_r($e);
                adodb_backtrace($e->gettrace());
            }

            //$ok = $user->Save();
           // if (!$ok) echo $err = $user->ErrorMsg();

           // var_dump($user->getAttributeNames());
            
            
        }
    }

   /*
require_once('libraries/adodb5/adodb-active-record.inc.php');
class User1 extends ADOdb_Active_Record{
    var $_table = 'aea_user';

}
class Profile extends ADOdb_Active_Record{
    var $_table = 'aea_profile';

}
*/